import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "login" */ '../components/Login/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    meta:{title:"首页"},
    component: () => import(/* webpackChunkName: "about" */ '../components/Home'),
    redirect:'/welcome',
    children:[
      {path: '/welcome',name: 'welcome', meta:{title:"欢迎"},component: ()=> import('../components/welcome.vue')},
      {path: '/users',name: 'users', meta:{title:"用户列表"}, component: ()=> import('../components/Users')},
      {path: '/rights',name: 'rights', meta:{title:"权限列表"}, component: ()=> import('../components/Power/Rights.vue')},
      {path: '/roles',name: 'roles', meta:{title:"角色列表"}, component: ()=> import('../components/Power/Roles.vue')},
      {path: '/categories',name: 'categories', meta:{title:"商品列表"}, component: ()=> import('../components/Goods/Cate.vue')},
      {path: '/goods',name: 'goods', meta:{title:"商品列表"}, component: ()=> import('../components/Goods/Goods.vue')},
      {path: '/params',name: 'params', meta:{title:"分类参数"}, component: ()=> import('../components/Goods/Params.vue')},
      {path: '/goods/add',name: 'add', meta:{title:"商品分类"}, component: ()=> import('../components/Goods/Add.vue')},
      {path: '/orders',name: 'orders', meta:{title:"订单列表"}, component: ()=> import('../components/Order/orders.vue')},
      {path: '/reports',name: 'reports', meta:{title:"数据统计"}, component: ()=> import('../components/Reports/reports.vue')},

  

    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})



// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将访问哪一个路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数,表示放行
  //   next() 放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

//   还应 后端验证 token 的真伪,



axios.interceptors.request.use(config => {
  // NProgress.start()
  // 登录授权 请求验证是否有token  需要授权的 API ，必须在请求头中使用 `Authorization` 字段提供 `token` 令牌
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config // 必须返回否则没有值
})



export default router
